#include "ADC.h"
#include "BH1750.h"
#include "DS18B20.h"
#include "GYBMP280.h"
#include "key.h"
#include "motor.h"
#include "OLED.h"
#include "pump.h"
#include "RGB.h"
#include "SHT30.h"
#include "WiFi.h"

static void agricultureTask(void)
{
    GYBMP280_Init();
    Key_Init();
    RGB_Init();
    OLED_Init();
    Motor_Init();
    Pump_Init();
    RGB_ROn();
    while(true)
    {
        OLED_ShowString(1, 1, "DS18B20");
        OLED_ShowChinese(1, 9, "初始化中");
        if(DS18B20_Init())
        {
            break;
        }
    } 
    OLED_ShowString(1, 1, "DS18B20");
    OLED_ShowChinese(1, 9, "初始化毕");

    while(true)
    {
        OLED_ShowString(2, 1, "BH1750");
        OLED_ShowChinese(2, 9, "初始化中");
        if(BH1750_Init())
        {
            break;
        }
    }
    OLED_ShowString(2, 1, "BH1750");
    OLED_ShowChinese(2, 9, "初始化毕");

    while(true)
    {
        OLED_ShowString(3, 1, "SHT30");
        OLED_ShowChinese(3, 9, "初始化中");
        if(SHT30_Init())
        {
            break;
        }        
    }
    OLED_ShowString(3, 1, "SHT30");
    OLED_ShowChinese(3, 9, "初始化毕");

    while(true)
    {
        OLED_ShowString(4, 1, "WiFi");
        OLED_ShowChinese(4, 9, "正在连接");
        if(WiFi_Connect())
        {
            break;
        }
    }
    OLED_ShowString(4, 1, "WiFi");
    OLED_ShowChinese(4, 9, "连接成功");
    usleep(2500*1000);
    OLED_Clear();
    RGB_ROff();
    while (1)
    {
        float lightIntensity, soilTemperature, atmosphericTemperature, atmosphericHumidity;
        if(BH1750_GetData(&lightIntensity) & DS18B20_GetData(&soilTemperature) & SHT30_GetData(&atmosphericTemperature, &atmosphericHumidity))
        {
            OLED_ShowChinese(1, 1, "光照强度");
            OLED_ShowCharacter(1, 9, ':');
            OLED_ShowInteger(1, 11, lightIntensity, 4);
            OLED_ShowString(1, 15, "lx");

            OLED_ShowChinese(2, 1, "水域温度");
            OLED_ShowCharacter(2, 9, ':');
            OLED_ShowSignedFloat(2, 10, soilTemperature, 2, 1);
            OLED_ShowChinese(2, 15, "℃");

            OLED_ShowChinese(3, 1, "大气温度");
            OLED_ShowCharacter(3, 9, ':');
            OLED_ShowSignedFloat(3, 10, atmosphericTemperature, 2, 1);
            OLED_ShowChinese(3, 15, "℃");

            OLED_ShowChinese(4, 1, "大气湿度");
            OLED_ShowCharacter(4, 9, ':');
            OLED_ShowInteger(4, 11, atmosphericHumidity, 2);
            OLED_ShowString(4, 13, " %RH");

            
        }

        usleep(5000*1000);
        OLED_Clear();

        float smokeDensity = ADC_GetSmokeDensity();
        float liquidHeight = ADC_GetLiquidHeight();
        float soilMoisture = ADC_GetSoilMoisture();
        double atmosphericPressure = GYBMP280_GetAtmosphericPressure();

        OLED_ShowChinese(1, 1, "烟雾浓度");
        OLED_ShowCharacter(1, 9, ':');
        //OLED_ShowInteger(1, 10, ADC_GetData(WIFI_IOT_ADC_CHANNEL_4), 4);
        OLED_ShowInteger(1, 11, smokeDensity, 3);
        OLED_ShowString(1, 14, "ppm");

        OLED_ShowChinese(2, 1, "水位高度");
        OLED_ShowCharacter(2, 9, ':');
        //OLED_ShowInteger(2, 10, ADC_GetData(WIFI_IOT_ADC_CHANNEL_1), 4);
        OLED_ShowSignedFloat(2, 10, liquidHeight, 2, 1);
        OLED_ShowString(2, 15, "mm");
        OLED_ShowCharacter(2, 10, ' ');
        
        OLED_ShowChinese(3, 1, "土壤湿度");
        OLED_ShowCharacter(3, 9, ':');
        //OLED_ShowInteger(3, 10, ADC_GetData(WIFI_IOT_ADC_CHANNEL_6), 4);
        OLED_ShowInteger(3, 11, soilMoisture, 2);
        OLED_ShowString(3, 14, "%RH");

        OLED_ShowChinese(4, 1, "大气压强");
        OLED_ShowCharacter(4, 9, ':');
        OLED_ShowInteger(4, 11, atmosphericPressure/1000, 3);
        OLED_ShowString(4, 14, "kPa");
        
        usleep(5000*1000);
        OLED_Clear();

        if(atmosphericTemperature > 28 && smokeDensity > 100)
        {
            RGB_GOn();
            Motor_On();
        }
        else
        {
            RGB_GOff();
            Motor_Off();
        }

        if(soilMoisture < 68 || liquidHeight > 20)
        {
            RGB_GOn();
            //Pump_On();
        }
        else
        {
            RGB_GOn();
            //Pump_Off();
        }
    }
}

static void agricultureEntry(void)
{
    osThreadAttr_t attr;

    attr.name = "agricultureTask";
    attr.stack_size = 4096*4;
    attr.priority = osPriorityNormal1;

    if (osThreadNew((osThreadFunc_t)agricultureTask, NULL, &attr) == NULL)
    {
        printf("Falied to create agricultureTask!\n");
    }
}

APP_FEATURE_INIT(agricultureEntry);
